package android_rteditor.customsortricheditor;

import android.animation.LayoutTransition;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import android_rteditor.customsortricheditor.editor.EditorDiplomat;
import android_rteditor.customsortricheditor.editor.RichAudio;
import android_rteditor.customsortricheditor.editor.RichEditorController;
import android_rteditor.customsortricheditor.editor.RichImage;

/**
 * Created by chenxunlin01 on 2017/8/16.
 */

public class RichEditor extends ScrollView {

    private LinearLayout root;
    private RichEditorController controller;
    private EditorDiplomat editorDiplomat;
    /**
     * 添加或者删除图片View时的Transition动画
     */
    private LayoutTransition mTransitioner;

    public RichEditor(Context context) {
        super(context);
        init();
    }

    public RichEditor(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RichEditor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initRootLayout();
        initEditorController();
        initContainerLayout();
    }

    /**
     * 根布局初始化
     */
    private void initRootLayout() {
        root = new LinearLayout(getContext());
        root.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        root.setDividerDrawable(getResources().getDrawable(R.drawable.editor_line_divider_shape));
        root.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        int padding = getResources().getDimensionPixelOffset(R.dimen.editor_padding);
        root.setPadding(padding, padding,padding, padding);
        addView(root, lp);
        mTransitioner = new LayoutTransition();
        mTransitioner.setDuration(100);
        root.setLayoutTransition(mTransitioner);
    }

    /**
     * 控制器初始化
     */
    private void initEditorController() {
        controller = new RichEditorController(this);
        controller.init();
        editorDiplomat = new EditorDiplomat() {
            @Override
            public void insertImage(String path) {
                controller.insertImage(path);
            }

            @Override
            public void insertCheckbox() {
                controller.insertCheckbox();
            }

            @Override
            public void removeRichImage(RichImage richImage) {
                controller.removeRichImage(richImage);
            }

            @Override
            public void removeRichAudio(RichAudio richAudio) {
                controller.removeRichAudio(richAudio);
            }

            @Override
            public void insertAudio() {
                controller.insertAudio();
            }
        };
    }

    /**
     * 获取对外接口
     *
     * @return
     */
    public EditorDiplomat getEditorDiplomat() {
        return editorDiplomat;
    }

    /**
     * 容器初始化
     */
    private void initContainerLayout() {
        controller.addEditorTextItem(0);
    }

    public LinearLayout getRoot() {
        return root;
    }
}
